﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Unitity;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Spinning.Spinning.Entity.Items
{
    class StaticBarrier : Actor
    {
        public StaticBarrier()
        {
            Physic_Type = PhysicType.Barrier;
            ZIndex = Constants.item;
            isCircle = false;
        }

        public int ItemLevel
        {
            get;
            set;
        }

        public bool isCircle
        {
            get;
            set;
        }

        public override void OnCreatBody(World world, Unitity.PhysicConnectManager connectManager)
        {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.Static;
            body = world.CreateBody(bd);
            FixtureDef fd = new FixtureDef();
            if (isCircle)
            {
                CircleShape cs = new CircleShape();//圓
                cs._radius = _frameSize.X / 2.45f / Constants.MeterToPixal;
                fd.shape = cs;
            }
            else
            {
                PolygonShape ps = new PolygonShape();//方
                ps.SetAsBox(_frameSize.X / 2.45f / Constants.MeterToPixal, _frameSize.Y / 2.45f / Constants.MeterToPixal);
                fd.shape = ps;
            }
            _fixture = body.CreateFixture(fd);
            body.SetTransform(Vector2.Zero, 0);
            PhysicConnector connector = new PhysicConnector(this, body);
            connectManager.AddPhysicConnector(connector);

            Filter filter = new Filter();
            filter.categoryBits = (ushort)Constants.CategoryBitsItem;
            filter.maskBits = (ushort)Constants.MaskBitsItem;
            Filter = filter;
            _fixture.SetRestitution(0.5f);
            _fixture.SetDensity(Constants.enemyDensity);
        }

        protected bool istouched = false;

        public override void BeginContact(PhysicSprite other)
        {
            base.BeginContact(other);
            
            if (other.Physic_Type == PhysicType.Player && !istouched)
            {
                SingleGamePlayer player = (SingleGamePlayer)other;
                OnTounched(player);
                //Alpha = 0;
                //istouched = true;
            }
        }

        public virtual void OnTounched(SingleGamePlayer player)
        {
            player.MinusLifeTime(ItemLevel);
        }

        int tempTimer;
        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            int delta = gameTime.ElapsedGameTime.Milliseconds;
            
            //if (istouched)
            //{
            //    tempTimer += delta;
                
            //    if (tempTimer / 300 > 1)  //1秒
            //    {
            //        this.BodyPosiotion = new Vector2(body.Position.X-2000, body.Position.Y);
            //    }
            //}
        }

    }
}
